package controllers

import (
	"github.com/astaxie/beego"
	_ "github.com/go-sql-driver/mysql"
	"html/template"
	"src/gitee.com/wx_e6924dbcd0/notebook/models"
	"strconv"
)

type MegMainController struct {
	beego.Controller
}

func (c *MegMainController) Get() {
	c.TplName = "base_login.html"

}
func (c *MegMainController) AddMessagePage() {
	//c.TplName = "base.html"
	//c.TplName = "message_board.html"
	c.TplName = "message.html"
	// 提交数据 //http://127.0.0.1:8080/处理

}

/**
*获取信息列表
**/
func (c *MegMainController) MegList() {
	c.TplName = "message_tb.html"
}

//http://127.0.0.1:8080/getmeglist?offset=0&limit=2
func (c *MegMainController) GetMegList() {
	// 读取数据库内容
	// 引用model,->orm
	// orm -> 引用 conf 配置连接DB
	// orm 读取数据库
	//template.HTMLEscapeString() 防xss漏洞
	result := make(map[string]interface{})
	result["msgcount"] = 0
	stuStr := template.HTMLEscapeString(c.Input().Get("stu"))
	stu, err := strconv.Atoi(stuStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + err.Error())
		return
	}
	//获取 offset 转成int 类型
	offsetStr := template.HTMLEscapeString(c.Input().Get("offset"))
	offset, err := strconv.Atoi(offsetStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + err.Error())
		return
	}
	//获取limit 转成int 类型
	limitStr := template.HTMLEscapeString(c.Input().Get("limit"))
	limit, err := strconv.Atoi(limitStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + err.Error())
		return
	}
	megList, err := models.GetNoteMegList(offset, limit, int64(stu))
	if err != nil {
		result["msg"] = "GetNoteMegList failed" + err.Error()
		log := models.UitlLog()
		log.Error("GetNoteMegList failed" + err.Error())
		return
	}
	var count = models.GetMegCount(int64(stu)) //获取总数
	// userId and message id check thumbup
	//获取点赞列表
	thumbupIdList, _ := models.GetThumbList()
	result["msgcount"] = count
	result["data"] = megList
	result["thumbupIdList"] = thumbupIdList

	c.Data["json"] = result
	c.ServeJSON()
}

/**
*根据id获取一篇文章
**/
//http://127.0.0.1:8080/
func (c *MegMainController) GetOneMessage() {
	msgId := template.HTMLEscapeString(c.Input().Get("msg_id"))
	id, err := strconv.Atoi(msgId)
	if err != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + err.Error())
		return
	}
	OneMsg := models.GetMsgByMid(int64(id))
	c.Data["json"] = OneMsg
	c.ServeJSON()
	return
}

/**
*根据当天获取文章
**/
//http://127.0.0.1:8080/
func (c *MegMainController) NowDaysMessage() {
	result := make(map[string]interface{})
	stuStr := "1"
	stu, err := strconv.Atoi(stuStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + err.Error())
		return
	}
	//获取 offset 转成int 类型
	offsetStr := template.HTMLEscapeString(c.Input().Get("offset"))
	offset, err := strconv.Atoi(offsetStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + err.Error())
		return
	}
	//获取limit 转成int 类型
	limitStr := template.HTMLEscapeString(c.Input().Get("limit"))
	limit, err := strconv.Atoi(limitStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + err.Error())
		return
	}
	megList, count, err := models.GetNowadaysMessage(offset, limit, stu)
	if err != nil {
		result["msg"] = "GetNowadaysMessage failed" + err.Error()
		log := models.UitlLog()
		log.Error("GetNowadaysMessage failed" + err.Error())
		return
	}
	// userId and message id check thumbup
	thumbupIdList, err := models.GetThumbList() //获取点赞列表
	if err != nil {
		result["msg"] = "GetThumbList failed" + err.Error()
		log := models.UitlLog()
		log.Error("GetThumbList failed" + err.Error())
		return
	}
	result["msgcount"] = count
	result["data"] = megList
	result["thumbupIdList"] = thumbupIdList
	c.Data["json"] = result
	c.ServeJSON()
	return
}

/**
*留言
**/
//http://127.0.0.1:8080/main/addmessage?name=mo&passwd=99&sex=%E5%A5%B3&content=qwqwq
func (c *MegMainController) DoAddMessage() {

	// 引用model,->orm
	// orm -> 引用 conf 配置连接DB
	// orm 插入数据到数据库
	hdline := template.HTMLEscapeString(c.Input().Get("hdline"))
	text := template.HTMLEscapeString(c.Input().Get("text"))
	name := template.HTMLEscapeString(c.Input().Get("name"))
	emil := template.HTMLEscapeString(c.Input().Get("emil"))
	remark := template.HTMLEscapeString(c.Input().Get("remark"))
	headpic := template.HTMLEscapeString(c.Input().Get("headpic"))
	idStr := template.HTMLEscapeString(c.Input().Get("id"))
	userid, err := strconv.Atoi(idStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("int64类型转换错误" + err.Error())
		return
	}
	if name != "" && hdline != "" && text != "" && emil != "" && remark != "" {
		id, err := models.MessageAdd(hdline, text, name, emil, remark, headpic, int64(userid))
		result := make(map[string]interface{})
		result["id"] = id
		if err != nil {
			result["msg"] = "AddMessage failed" + err.Error()
			log := models.UitlLog()
			log.Error("AddMessage failed" + err.Error())
			return
		} else {
			result["msg"] = "OK"
		}

		c.Data["json"] = result
		c.Ctx.SetCookie("addmsg", "1", 2)
		c.TplName = "base_login.html"
		//c.ServeJSON()
		return

	}
	//c.ServeJSON()
	c.TplName = "message.html"
	//
	return

	// 返回json 数据， 页面 通过js location 跳到 评论列表 //http://127.0.0.1:8080/main/getmessagelist

}

/**
*删除一条信息
**/
//http://127.0.0.1:8080/delmsg?id=15
func (c *MegMainController) DelMsgById() {
	// 读取数据库内容
	// 引用model,->orm
	// orm -> 引用 conf 配置连接DB
	// orm 读取数据库

	idStr := template.HTMLEscapeString(c.Input().Get("id"))
	id, err := strconv.Atoi(idStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("string 转int64 err"+idStr, err.Error())
		return
	}
	result := make(map[string]interface{})
	//执行删除操作
	delNubmer, err := models.DelMsg(int64(id))
	if err != nil {
		result["count"] = 0
		result["msg"] = "DelMsg failed" + err.Error()
		log := models.UitlLog()
		log.Error("DelMsg failed", err.Error())
		return
	}
	// 成功返回数据
	result["count"] = delNubmer
	result["msg"] = "ok"
	c.Data["json"] = result
	c.ServeJSON()
}

/**
*修改文章状态
**/
//http://127.0.0.1:8080/delmsg?id=15
func (c *MegMainController) EditMsgStatus() {
	numStr := template.HTMLEscapeString(c.Input().Get("num"))
	num, err := strconv.Atoi(numStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("string 转int64 err"+numStr, err.Error())
		return
	}
	idStr := template.HTMLEscapeString(c.Input().Get("id"))
	id, err := strconv.Atoi(idStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("string 转int64 err"+idStr, err.Error())
		return
	}
	result := make(map[string]interface{})
	//执行删除操作
	erredil := models.UpMsgStayus(int64(num), int64(id))
	if erredil != nil {
		result["UpMsgStatue"] = "UpMsgStayus failed" + erredil.Error()
		log := models.UitlLog()
		log.Error("UpMsgStayus failed", erredil.Error())
		return
	}
	// 成功返回数据
	result["UpMsgStatue"] = "ok"
	c.Data["json"] = result
	c.ServeJSON()
}

/**
*更新信息
**/
//http://127.0.0.1:8080/upmessage?id=4&hdline=hh&text=%E53&emil=126&remark=jjppo
func (c *MegMainController) UpMsge() {
	// 读取修改数据库内容
	// 引用model,->orm
	// orm -> 引用 conf 配置连接DB
	// orm 读取修改数据库
	idStr := template.HTMLEscapeString(c.Input().Get("m_id"))
	id, err := strconv.Atoi(idStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("string 转int64 err"+idStr, err.Error())
		return
	}
	hdline := c.Input().Get("hdline")
	text := c.Input().Get("text")
	emil := c.Input().Get("emil")
	remark := c.Input().Get("remark")
	if hdline != "" && text != "" && emil != "" && remark != "" {
		// orm insert
		err := models.UpMsg(int64(id), hdline, text, emil, remark)
		if err != nil {
			log := models.UitlLog()
			log.Error("UpMsg 错误", err.Error())
			return
		}
		c.Ctx.SetCookie("Upmsg", "ok")
		//c.ServeJSON()
		c.TplName = "up_message.html"
		return

	}
}

/**
*模糊查询
**/ //http://127.0.0.1:8080/fuzzymsg?value=伟大&limit=3&offset=0
func (c *MegMainController) Fuzzy() {

	// 读取修改数据库内容
	// 引用model,->orm
	// orm -> 引用 conf 配置连接DB
	// orm 读取修改数据库
	value := template.HTMLEscapeString(c.Input().Get("value"))
	result := make(map[string]interface{})
	result["msgcount"] = 0
	//获取 offset 转成int 类型
	offsetStr := template.HTMLEscapeString(c.Input().Get("offset"))
	offset, err := strconv.Atoi(offsetStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("string 转int64 err", err.Error())
		return

	}
	//获取limit 转成int 类型
	limitStr := template.HTMLEscapeString(c.Input().Get("limit"))
	limit, err := strconv.Atoi(limitStr)
	if err != nil {
		log := models.UitlLog()
		log.Error("string 转int64 err", err.Error())
		return
	}
	if value != "" {
		// orm insert
		data, err := models.FuzzyQry(value, offset, limit)
		if err != nil {
			result["error"] = "FuzzyQry failed" + err.Error()
			log := models.UitlLog()
			log.Error("FuzzyQry failed", err.Error())
			return
		} else {
			result["data"] = data
		}
		//获取点赞列表
		thumbupIdList, err := models.GetThumbList()
		if err != nil {
			log := models.UitlLog()
			log.Error("GetThumbList failed", err.Error())
			return
		}
		var count = models.FuzzyQsCount(value) //获取查询用户数
		result["msgcount"] = count
		result["data"] = data
		result["thumbupIdList"] = thumbupIdList
		c.Data["json"] = result
		c.ServeJSON()
		//return
	}
}
